from django.shortcuts import render
from rest_framework.generics import GenericAPIView
from django_redis import get_redis_connection
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from goods.models import SKU


class OrderPlaceView(GenericAPIView):
    serializer_class = ""
    permission_classes = [IsAuthenticated]


    def get(self, request):
        # 1.获取redis中需要结算
        user = request.user
        redis_conn = get_redis_connection("cart")
        cart_dict = redis_conn.hgetall("cart_%s" % user.id)
        cart_selected_list = redis_conn.smembers("cart_selects_%s" % user.id)

        # 2.拼接数据
        sku_list = []
        for sku_id in cart_selected_list:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)

        # 3.获取序列化器
        serializer = self.get_serializer(instance=sku_list, many=True)

        # 4.返回响应
        contenxt = {
            "skus": serializer.data,
            "freight": ""
        }

        return Response(contenxt)
